package com.ops.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

/**
 * Spring Security安全配置类
 * 负责系统的安全认证、授权等配置
 */
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private UserDetailsService userDetailsService;

    /**
     * 密码加密器配置
     * 使用BCrypt加密算法进行密码加密
     */
    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    /**
     * 认证管理器配置
     * 配置用户详情服务和密码加密器
     */
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userDetailsService)
            .passwordEncoder(passwordEncoder());
    }

    /**
     * HTTP安全配置
     * 配置请求授权规则、登录、登出等安全相关配置
     */
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .authorizeRequests()
                // 静态资源允许匿名访问
                .antMatchers("/css/**", "/js/**", "/images/**", "/excel/**").permitAll()
                // 登录和错误页面允许匿名访问
                .antMatchers("/login", "/error").permitAll()
                // API接口的访问控制
                .antMatchers("/api/inspections/{reportUuid}/{hospitalId}/{demandYear}",
                        "/api/tasks/{reportUuid}/{hospitalId}/{demandYear}/monthly-stats",
                        "/api/tasks/{reportUuid}/{hospitalId}/{demandYear}/monthly-workload",
                        "/api/tasks/{reportUuid}/{hospitalId}/{demandYear}/page",
                        "/api/faults/{reportUuid}/monthly-fault-stats").permitAll()
                // 报告相关页面的访问控制
                .antMatchers("/homePage/{uuid}", "/reports/{uuid}", "/reports/{uuid}/top-requests",
                        "/reports/{uuid}/inspection-stats", "/reports/{uuid}/thanks").permitAll()
                // 其他请求需要认证
                .anyRequest().authenticated()
                .and()
            // 登录配置
            .formLogin()
                .loginPage("/login")
                .defaultSuccessUrl("/")
                .failureUrl("/login?error")
                .and()
            // 登出配置
            .logout()
                .logoutSuccessUrl("/login?logout")
                .and()
            // 禁用CSRF保护
            .csrf().disable();
    }
} 